package com.gewu.ordlelist.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.gewu.ordlelist.MainActivity;
import com.gewu.ordlelist.R;
import com.gewu.ordlelist.activity.loginActivity;
import com.gewu.ordlelist.activity.mian;
import com.gewu.ordlelist.activity.mian2;
import com.gewu.ordlelist.base.BaseFragment;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.database.dbbean.footers;
import com.gewu.ordlelist.database.dbdao.ByCarDao;
import com.gewu.ordlelist.database.dbdao.UserDaoPerson;
import com.gewu.ordlelist.database.dbdao.footerDao;
import com.gewu.ordlelist.utils.actionOnClick;
import com.gewu.ordlelist.utils.jocUtils;

import java.util.List;
import java.util.Random;

public class fourFragment extends BaseFragment implements Toolbar.OnMenuItemClickListener {
    private Toolbar toolbar;
    private TextView text;
    private ImageView img;
    private TextView name;
    private TextView xiadan;
    private TextView ing;
    private TextView upto;
    private RecyclerView rec;

    @Override
    public View FindView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.four_fragment, null, false);
    }

    @Override
    public void initView(View view) {
        toolbar = view.findViewById(R.id.toolbar);
        text = view.findViewById(R.id.text);
        img = view.findViewById(R.id.img);
        name = view.findViewById(R.id.name);
        xiadan = view.findViewById(R.id.xiadan);
        ing = view.findViewById(R.id.ing);
        upto = view.findViewById(R.id.upto);
        rec = view.findViewById(R.id.rec);
        toolbar.inflateMenu(R.menu.menu3);
        toolbar.setTitle(setToolbarText());
        toolbar.setOnMenuItemClickListener(this);

        RoundedCorners roundedCorners = new RoundedCorners(50);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        int i = new Random().nextInt(6);
        Glide.with(getContext()).load(jocUtils.getimgs()[i]).apply(options).into(img);
        name.setText(jocUtils.name==null ? "null":jocUtils.name);

        xiadan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), mian.class));
            }
        });

        upto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), mian2.class));
            }
        });


        setAdapter();
    }

    @Override
    public void setAdapter() {
        List<footers> quer = new footerDao(getContext()).quer();
        rec.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        rec.setAdapter(new RecyclerView.Adapter() {

            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.four_item, parent, false);
                return new fourItem(inflate);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                RoundedCorners roundedCorners = new RoundedCorners(50);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                int i = new Random().nextInt(6);
                Glide.with(holder.itemView).load(jocUtils.getimgs()[i]).apply(options).into(((fourItem) holder).image);
                ((fourItem) holder).text.setText(quer.get(position).getName() + "\t价格：" + quer.get(position).getMoney());
                ((fourItem) holder).text2.setText(R.string.text);
                ((fourItem) holder).ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new footerDao(holder.itemView.getContext()).delectFoId(quer.get(position));
                        setAdapter();
                    }
                });
            }

            @Override
            public int getItemCount() {
                return quer.size();
            }
        });
    }

    @Override
    public String setToolbarText() {
        return "服务中心";
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if(item.getItemId()==R.id.menu_one){
            jocUtils.showDilog(getContext(),"提示","您确定退出？").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    jocUtils.isok(getContext(), false);
                    startActivity(new Intent(getContext(), loginActivity.class));
                    dialog.dismiss();
                }
            }).setNegativeButton("no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
        }
        return false;
    }

    private class fourItem extends RecyclerView.ViewHolder {

        private TextView text2;
        private TextView text;
        private ImageView image;
        private TextView ok;

        public fourItem(View inflate) {
            super(inflate);
            image = inflate.findViewById(R.id.image);
            text = inflate.findViewById(R.id.text);
            text2 = inflate.findViewById(R.id.text2);
            ok = inflate.findViewById(R.id.ok);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        setAdapter();
    }


}
